/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.article.controller;

import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.jzwl.common.constant.AjaxStatusConstant;
import com.jzwl.common.constant.Constants;
import com.jzwl.common.page.PageObject;
import com.jzwl.common.upload.SftpChannel;
import com.jzwl.common.upload.service.UploadImgService;
import com.jzwl.site.manager.article.service.SiteArticleService;
import com.jzwl.system.base.controller.BaseController;

/**
 * SiteNodeType
 * SiteNodeType
 * siteNodeType
 * <p>Title:SiteNodeTypeController </p>
 * 	<p>Description:SiteNodeType </p>
 * 	<p>Company: </p> 
 * 	@author aotu-code 
 */
@Controller
@RequestMapping("/siteArticle")
public class SiteArticleController extends BaseController {

	@Autowired
	private SiteArticleService siteArticleService;
	@Autowired
	private UploadImgService uploadImgService;
	@Autowired
	private Constants constants;

	private static Logger log = LoggerFactory.getLogger(SiteArticleController.class);

	/**
	 * 跳转到添加SiteNodeType的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/articleIndex")
	public ModelAndView articleIndex(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);

		mov.setViewName("/manager/article/articleindex");
		return mov;
	}

	/**
	 * 跳转到添加SiteNodeType的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toAddArticle")
	public ModelAndView toAddsiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		mov.addObject("nodeId", paramsMap.get("nodeId"));
		mov.setViewName("/manager/article/add");
		return mov;
	}

	/**
	 * 跳转到添加SiteNodeType的页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toFirstAdd")
	public ModelAndView toFirstAdd(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		mov.addObject("nodeId", paramsMap.get("nodeId"));
		mov.setViewName("/manager/article/firstAdd");
		return mov;
	}

	/**
	 * 添加SiteNodeType
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addArticle")
	public ModelAndView addsiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);

		try {
			paramsMap.put("isTop", 0);

			boolean flag = siteArticleService.addSiteArticle(paramsMap);
			if (!flag) {
				mov.addObject("msg", "添加SiteNodeType失败!");
				mov.setViewName("/error");

			} else {
				mov.setViewName("redirect:/siteArticle/siteArticleList.html?nodeId=" + paramsMap.get("nodeId"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "添加SiteNodeType失败!");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 添加SiteNodeType
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addFirst")
	public ModelAndView addFirst(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);

		try {
			paramsMap.put("isTop", 0);
			paramsMap.put("title", "");
			paramsMap.put("subtitle", "");
			paramsMap.put("author", "");
			paramsMap.put("isRecom", 0);

			boolean flag = siteArticleService.addSiteArticle(paramsMap);
			if (!flag) {
				mov.addObject("msg", "添加SiteNodeType失败!");
				mov.setViewName("/error");

			} else {
				mov.setViewName("redirect:/siteArticle/siteArticleList.html?nodeId=" + paramsMap.get("nodeId"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "添加SiteNodeType失败!");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * siteNodeTypelist
	 */
	@RequestMapping(value = "/siteArticleList")
	public ModelAndView siteNodeTypeList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		PageObject po = new PageObject();
		try {
			paramsMap.put("isDelete", 0);
			paramsMap.put("articleType", 1);
			po = siteArticleService.querySiteArticleList(paramsMap);

			mov.addObject("list", po.getDatasource());
			mov.addObject("totalProperty", po.getTotalCount());
			mov.addObject("page", po);
			mov.addObject("nodeId", paramsMap.get("nodeId"));

			mov.addObject("title", paramsMap.get("title"));
			mov.addObject("author", paramsMap.get("author"));
			mov.addObject("beginDate", paramsMap.get("beginDate"));
			mov.addObject("endDate", paramsMap.get("endDate"));

			mov.addObject("isRecom", paramsMap.get("isRecom"));
			mov.addObject("isTop", paramsMap.get("isTop"));
			mov.addObject("isPub", paramsMap.get("isPub"));

			mov.setViewName("/manager/article/list");
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "Article查询失败");
			mov.setViewName("/error");
		}
		return mov;
	}

	/**
	 * 跳转到修改siteNodeType页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toUpdateArticle")
	public ModelAndView toUpdateSiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);

		try {
			Map article = siteArticleService.getById(paramsMap);
			//图片服务器路径
			mov.addObject("projectPath", constants.getSftz_image_path());
			mov.addObject("obj", article);
			mov.setViewName("/manager/article/edit");
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "siteNodeType查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 修改首页的内容
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toFirstEdit")
	public ModelAndView toFirstEdit(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		try {
			Map article = siteArticleService.getInfoByNodeId(paramsMap);
			//图片服务器路径
			mov.addObject("projectPath", constants.getSftz_image_path());
			mov.addObject("obj", article);
			mov.addObject("nodeId", paramsMap.get("nodeId"));
			mov.setViewName("/manager/article/firstEdit");
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "siteNodeType查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 
	 * 项目名称：sftj-offical-site
	 * 描述：预览内容
	 * 创建人： ln
	 * 创建时间： 2016年11月29日
	 * 标记：
	 * @param request
	 * @param response
	 * @return
	 * @version
	 */
	@RequestMapping(value = "/previewContent")
	public ModelAndView previewContent(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);
		try {
			Map article = siteArticleService.getById(paramsMap);
			mov.addObject("article", article);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mov.setViewName("/wap/previewList");
		return mov;
	}

	/**
	 * 修改siteNodeType
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateArticle")
	public ModelAndView updateSiteNodeType(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);

		try {
			paramsMap.put("pubTime", new Date());
			Object articleId = paramsMap.get("articleId");
			if ("".equals(articleId)) {//新增
				paramsMap.put("isTop", 0);
				paramsMap.put("title", "");
				paramsMap.put("subtitle", "");
				paramsMap.put("author", "");
				paramsMap.put("isRecom", 0);

				boolean flag = siteArticleService.addSiteArticle(paramsMap);
				if (!flag) {
					mov.addObject("msg", "添加SiteNodeType失败!");
					mov.setViewName("/error");

				} else {
					mov.setViewName("redirect:/siteArticle/siteArticleList.html?nodeId=" + paramsMap.get("nodeId"));
				}
			} else {
				boolean flag = siteArticleService.updateSiteArticle(paramsMap);
				if (!flag) {
					mov.addObject("msg", "修改失败!");
					mov.setViewName("/error");
				} else {
					mov.setViewName("redirect:/siteArticle/siteArticleList.html?nodeId=" + paramsMap.get("nodeId"));
				}
			}

		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "修改失败!");
			mov.setViewName("/error");
		}
		return mov;
	}

	/**
	 * 发布
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/pubArticle")
	public @ResponseBody Map<String, Object> topNodeTree(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
		createParameterMap(request);
		try {
			paramsMap.put("pubTime", new Date());
			boolean flag = siteArticleService.pubArticle(paramsMap);
			if (!flag) {
				resultMap.put("msg", "修改失败!");
			} else {
				resultMap.put("error", "获取栏目数异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("error", "获取栏目数异常");
		}
		return resultMap;
	}

	@RequestMapping(value = "/setValue")
	public @ResponseBody Map<String, Object> setValue(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
		createParameterMap(request);
		try {
			paramsMap.put(paramsMap.get("name").toString(), paramsMap.get("value"));
			boolean flag = siteArticleService.setValue(paramsMap);
			if (!flag) {
				resultMap.put("msg", "修改失败!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("error", "获取栏目数异常");
		}
		return resultMap;
	}

	/**
	 * 逻辑删除siteNodeType
	 * @param request
	 * @param response
	 * @return
	 */

	@RequestMapping(value = "/delArticle")
	public @ResponseBody Map<String, Object> delArticle(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
		createParameterMap(request);
		try {
			boolean flag = siteArticleService.delSiteArticle(paramsMap);
			if (!flag) {
				resultMap.put("msg", "删除失败!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("error", "获取栏目数异常");
		}
		return resultMap;
	}

	@RequestMapping(value = "/delSiteArticle")
	public ModelAndView delSiteNodeType(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteArticleService.delSiteArticle(paramsMap);

			if (!flag) {
				mov.addObject("msg", "删除失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteArticle/siteArticleList.html?nodeId=" + paramsMap.get("nodeId"));
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "删除失败!");
			mov.setViewName("/error");

		}

		return mov;
	}

	/**
	 * 物理删除siteNodeType
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/deleteSiteArticle")
	public ModelAndView deleteSiteNodeType(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mov = new ModelAndView();

		createParameterMap(request);

		try {

			boolean flag = siteArticleService.deleteSiteArticle(paramsMap);

			if (!flag) {
				mov.addObject("msg", "删除失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteArticle/siteArticleList.html?nodeId=" + paramsMap.get("nodeId"));
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "删除失败!");
			mov.setViewName("/error");

		}

		return mov;
	}

	/////////////////////////////////////////////////////////////
	@RequestMapping(value = "uploadFirstImage", method = RequestMethod.POST)
	public @ResponseBody Map uploadFirlstImage(@RequestParam("img") MultipartFile img, HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		if (img.isEmpty()) {
			map.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_FAILURE);
			map.put("msg", "图片不能为空！");
			return map;
		}
		log.info("++++++++++++++++++++++++++++++++进入上传 图片");
		try {
			//获取文章的后缀
			String filename = URLEncoder.encode(img.getOriginalFilename(), "utf-8");
			//String url = uploadImgService.uploadImg(1, img);//返回全路径
			//String url = FtpJSchUtil.upload(img);
			InputStream inStream = img.getInputStream();
			SftpChannel ftp = new SftpChannel();
			String url = ftp.upload(inStream, img.getOriginalFilename());
			map.put("picUrl", url);// 相对路径，存数据库的。
			map.put("targetPath", url);// 绝对路径，显示
			map.put("filename", filename);
			map.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_SUCCESS);
			return map;
		} catch (Exception e) {
			map.put("ajax_status", AjaxStatusConstant.AJAX_STATUS_FAILURE);
			map.put("msg", "上传失败！" + e.getMessage());
			e.printStackTrace();
			return map;
		}
	}

	@RequestMapping(value = "uploadDescImage", method = RequestMethod.POST)
	public void uploadDescImage(@RequestParam("imgFile") MultipartFile imgFile, HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		org.json.simple.JSONObject obj = new org.json.simple.JSONObject();
		try {
			out = response.getWriter();
			if (imgFile.isEmpty()) {
				obj.put("error", 1);
				obj.put("message", "图片不能为空！");
				out.println(obj.toJSONString());
			}
			//String url = uploadImgService.uploadImg(1, imgFile);//返回全路径
			InputStream inStream = imgFile.getInputStream();
			SftpChannel ftp = new SftpChannel();
			String url = ftp.upload(inStream, imgFile.getOriginalFilename());
			//发送给 KE 
			obj.put("error", 0);
			obj.put("url", url);// 绝对路径，显示
			out.println(obj.toJSONString());
		} catch (Exception e) {
			obj.put("error", 1);
			obj.put("message", "上传失败！" + e.getMessage());
			e.printStackTrace();
			out.println(obj.toJSONString());
		} finally {
			out.close();
		}
	}

	///////////////////////////////////////////////////////////////////////////////////
	@RequestMapping(value = "/toUpdateUs")
	public ModelAndView toUpdateUs(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);

		try {
			Map article = siteArticleService.getUsById();
			mov.addObject("obj", article);
			mov.setViewName("/manager/us/edit");
		} catch (Exception e) {
			e.printStackTrace();
			mov.addObject("msg", "siteNodeType查询失败");
			mov.setViewName("/error");
		}

		return mov;
	}

	/**
	 * 修改siteNodeType
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateUs")
	public ModelAndView updateUs(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mov = new ModelAndView();
		createParameterMap(request);

		try {
			boolean flag = siteArticleService.updateUs(paramsMap);

			if (!flag) {
				mov.addObject("msg", "修改失败!");
				mov.setViewName("/error");
			} else {
				mov.setViewName("redirect:/siteArticle/toUpdateUs.html?id=" + paramsMap.get("id"));
			}
		} catch (Exception e) {

			e.printStackTrace();
			mov.addObject("msg", "修改失败!");
			mov.setViewName("/error");
		}
		return mov;
	}
}
